<?php
class mysql {
	private $host; //数据库主机
	private $username; //数据库用户名
	private $password; //数据库用户名密码
	private $database; //数据库名
	private $port; //数据库端口
	private $charset; //数据库编码，GBK,UTF8,gb2312
	private $pconnect;
	private $log;
	
	private $conn; //数据库连接标识;
	private $result; //执行query命令的结果资源标识
	private $sql; //sql执行语句
	private $error;
	private $debug;
	/*构造函数*/
	public function __construct($database,$username,$password,$port='3306',$host='localhost',$charset='utf8',$pconnect=true,$log=null,$debug=false) {
		$this->database = $database;
		$this->username = $username;
		$this->password = $password;
		$this->port = $port;
		$this->host = $host;
		$this->charset = $charset;
		$this->pconnnect = $pconnect;
		$this->log = $log;
		$this->debug=$debug;
		
		$this->connect();
	}

	public function copy(){
		return new mysql(
			$this->database,
			$this->username,
			$this->password,
			$this->port,
			$this->host,
			$this->charset,
			$this->pconnnect,
			$this->log,
			$this->debug
		);
	}
	/*数据库连接*/
	private function connect() {
		if ($this->pconnect) {
			//永久链接
			$this->conn = mysql_pconnect($this->host.":".$this->port, $this->username, $this->password);
		} else {
			//即使链接
			$this->conn = mysql_connect($this->host.":".$this->port, $this->username, $this->password);
		}
		mysql_select_db($this->database, $this->conn);
		mysql_query("SET NAMES $this->charset");
	}
	
	public function charset_set($charset){
		$this->charset = $charset;
		mysql_query("SET NAMES $this->charset");
	}
	
	public function build_sql_select($tablename, $select = "*", $where = "1", $order=array(),$limit_clause="") {
		$sql = 'select '.$select.' from '.$tablename.' where '.$where;
		if(is_array($order) && count($order)>0){
			$sql.=" order by";
			foreach ($order as $k=>$v){
				$sql.=" ".$k." ".$v;
			}
		}
		$sql.=' '.$limit_clause;
		return $sql;
	}
	public function mysql_query($sql){
		$this->sql = $sql;
		$result = mysql_query($sql, $this->conn);
		$this->result=$result;
		if($result===false){
			$this->error = $this->mysql_error();
			if($this->log){
				$this->log->log("执行数据库操作失败!\n SQL: ".$sql."\n ERROR: ".$this->mysql_error());
			}
			if($this->debug){
				echo "<!DOCTYPE html>";
				echo "<html>";
				echo "<head>";
				echo "<meta charset=\"utf-8\">";
				echo "</head>";
				echo "<body><pre>";
				echo "执行数据库操作失败!\n SQL: ".$sql."\n ERROR: ".$this->mysql_error();
				echo "</pre></body></html>";
				die();
			}
		}
		return $result;
	}
	public function mysql_fetch_row(){
		return mysql_fetch_row($this->result);
	}
	public function mysql_fetch_assoc(){
		return mysql_fetch_assoc($this->result);
	}
	public function mysql_fetch_array(){
		return mysql_fetch_array($this->result);
	}
	public function mysql_num_rows($sql){
		return mysql_num_rows($this->result);
	}
	public function query_count($sql){
		$result = $this->mysql_query($sql);
		if($result===false){
			return false;
		}
		
		return mysql_num_rows($result);
	}
	public function query_row($sql){
		$result = $this->mysql_query($sql);
		if($result===false){
			return false;
		}
		
		$result = mysql_fetch_row($result);
		if($result===false){
			return null;
		}else{
			return $result;
		}
	}
	public function query_row_all($sql){
		$result = $this->mysql_query($sql);
		if($result===false){
			return false;
		}
		
		$array = array();
		while($row = mysql_fetch_row($result)){
			array_push($array,$row);
		}
		return $array;
	}
	
	public function query_assoc($sql){
		$result = $this->mysql_query($sql);
		if($result===false){
			return false;
		}
		
		$result = mysql_fetch_assoc($result);
		if($result===false){
			return null;
		}else{
			return $result;
		}
	}
	public function query_assoc_all($sql){
		$result = $this->mysql_query($sql);
		if($result===false){
			return false;
		}
		
		$array = array();
		while($row = mysql_fetch_assoc($result)){
			array_push($array,$row);
		}
		return $array;
	}
	public function query_first($sql){
		$row = $this->query_row($sql);
		if($row===false){
			return false;
		}else if($row!=null && count($row)>0){
			return $row[0];
		}else{
			return null;
		}
	}
	public function query_first_all($sql){
		$rows = $this->query_row_all($sql);
		if($rows===false){
			return false;
		}else{
			$res = array();
			foreach($rows as $row){
				array_push($res,$row[0]);
			}
			return $res;
		}
	}
	public function query_pair_all($sql){
		$result = $this->mysql_query($sql);
		if($result===false){
			return false;
		}
		
		$array = array();
		while($row = mysql_fetch_row($result)){
			$array[$row[0]]=$row[1];
		}
		return $array;
	}
	public function update($sql){
		$result = $this->mysql_query($sql);
		if($result===false){
			return false;
		}else{
			return true;
		}
	}
	
	public function mysql_insert_id(){
		return mysql_insert_id($this->conn);
	}
	public function mysql_affected_rows(){
		return mysql_affected_rows($this->conn);
	}
	public function mysql_error(){
		return mysql_error($this->conn);
	}
	
	/**
	 * 析构函数，自动关闭数据库,垃圾回收机制
	 * Enter description here ...
	 */
	public function __destruct() {
		if ($this->result) {
			@($this->result);
		}
		if($this->conn){
			@mysql_close($this->conn);
		}
	}
}
?>